package com.yc.springmvc.web;

import com.yc.springmvc.bean.Hall;
import com.yc.springmvc.bean.Movie;
import com.yc.springmvc.bean.Plan;
import com.yc.springmvc.bean.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * Controller : Controller 标注的类, 内部方法的返回值, 默认为视图名
 *          JPS , Thymeleaf, ...
 * RestControoller :   xxxx  默认方法的返回值是 json 格式数据
 */
@RestController
@RequestMapping("web/page")
public class HelloAction {

    @RequestMapping("hello.do")
    public String hello(){
        return "hello world";
    }

    // 接口注入
    // ModelAndView 表示该方法,将会跳转到指定页面(视图)
    @RequestMapping("tohome.do")
    ModelAndView toHome(ModelAndView modelAndView){
        // 响应重定向跳转到 home
        modelAndView.setViewName("redirect:/home.html");
        return modelAndView;
    }

    @RequestMapping(path = "get.do", method = RequestMethod.GET)
    Result get(){
        return new Result(1,"doGet", null);
    }

    @GetMapping("get1.do")
    Result get1(){
        return new Result(1,"doGet", null);
    }

    @RequestMapping(path = "getOrPost.do",
            method = {RequestMethod.GET,RequestMethod.POST})
    Result getOrPost(){
        return new Result(1,"getOrPost", null);
    }

    @GetMapping(path="login1", params = "email")
    Result loginWithEmail(String email){
        return new Result(1,"loginWithEmail", email);
    }

    @GetMapping(path="login1", params = "email=qq@qq.com")
    Result loginWithEmail1(String email){
        return new Result(1,"loginWithEmail1", email);
    }

    @GetMapping(path = "header.do", headers = "Host=localhost")
    String header1(){
        return "localhost";
    }

    @GetMapping(path = "header.do", headers = "Host=127.0.0.1")
    String header2(){
        return "127.0.0.1";
    }

    @PostMapping(path = "consumes.do",
            // 必须是表单提交请求
            consumes = MediaType.TEXT_PLAIN_VALUE)
    String consumes(){
        return "consumes " + MediaType.TEXT_PLAIN_VALUE;
    }

    @GetMapping(path="produces1.do")
    String produces1(){
        return "produces 中文" ;
    }

    // Content-Type
    @GetMapping(path="produces2.do",produces = MediaType.TEXT_HTML_VALUE+";charset=utf-8")
    String produces2(){
        return "produces 中文" ;
    }

    // 自动映射
    @GetMapping("login")
    String login2(String uname, String pwd, int vcode){
        return uname + "," + pwd + "," + vcode;
    }

    // 自动装箱
    @GetMapping("hall")
    Hall hall(Hall abc){
        return abc;
    }

    @GetMapping("plan")
    Plan plan(Plan abc){
        return abc;
    }

    @RequestMapping("movie")
    Movie movie(Movie abc ){
        return abc;
    }

    // @RequestBody 将请求头中的数据(json) 装载到 abc 中
    @RequestMapping("movie2")
    Movie movie2( @RequestBody  Movie abc ){
        return abc;
    }

    // 注入 Serlvet 相关对象
    @GetMapping("servletObjects.do")
    Set<Object> getServletObjects(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session,
            InputStream inputStream//,  // 请求的输入流
            //OutputStream outputStream, // 响应输出流
            //PrintWriter out
    ){
        Set<Object> ret = new LinkedHashSet<>();
        ret.add(request);
        ret.add(response);
        ret.add(session);
        ret.add(inputStream);
        //ret.add(outputStream);
        //ret.add(out);
        return ret;
    }

    @GetMapping("servletObjects1.do")
    String getServletObjects1(
            OutputStream out
    ){
        return out.toString();
    }












}

